<template>
  <div>
    <!--卡片-->
    <el-card class="box-card">
      <!--表格-->
      <el-table :data="listForm" stripe border style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="请求方法">
                <span>{{ props.row.method }}</span>
              </el-form-item>
              <el-form-item label="请求参数">
                <span>{{ props.row.content }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="操作人" />
        <el-table-column prop="title" label="描述" />
        <el-table-column prop="ip" label="ip" />
        <el-table-column prop="elapsedTime" label="耗时">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.elapsedTime <= 300">{{ scope.row.elapsedTime }}ms</el-tag>
            <el-tag v-else-if="scope.row.elapsedTime <= 1000" type="warning">{{ scope.row.elapsedTime }}ms</el-tag>
            <el-tag v-else type="danger">{{ scope.row.elapsedTime }}ms</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="browser" label="浏览器" />
        <el-table-column prop="createTime" label="操作日期" />
        <!--操作-->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" style="margin-right: 2px" type="text" @click="detailBtn(scope.row.id)">
              <el-link type="primary">查看错误</el-link>
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        background
        :current-page="pageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

      <!--错误详情-->
      <el-dialog title="错误详情" :visible.sync="detailDialogVisible" width="580px">
        <span>{{ form.errorMsg }}</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="detailDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getLog, listLog } from '@/api/monitor/log'

export default {
  name: 'ErrorLog',
  data() {
    return {
      detailDialogVisible: false,
      pageNumber: 1,
      pageSize: 10,
      total: 1,
      listForm: [],
      form: {}
    }
  },
  created() {
    // 初始化列表
    this.initListLog()
  },

  methods: {

    detailBtn(id) {
      this.detailDialogVisible = true
      getLog(id).then(res => {
        this.form = res.data
        console.log(this.form)
      })
    },
    // 监听当前页的事件
    handleCurrentChange(pageIndex) {
      const params = {
        pageNum: pageIndex,
        pageSize: this.pageSize
      }
      listLog(params).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
        this.pageSize = res.data.size
      })
    },

    // 监听当前页的条数事件
    handleSizeChange(pageIndex) {
      const params = {
        pageNum: this.pageNumber,
        pageSize: pageIndex
      }
      listLog(params).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
        this.pageSize = res.data.size
      })
    },

    // 初始化数据
    initListLog() {
      const params = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize,
        logType: 'error'
      }
      listLog(params).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    }
  }
}
</script>

<style scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 100%;
}
.demo-table-expand .el-form-item__content {
  font-size: 12px;
}
</style>
